﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using Quaack.Properties;

namespace Quaack
{
    public partial class PaignaLezenAndereGebruiker : System.Web.UI.Page
    {


        protected void page_preinit()
        {
            if (User.Identity.IsAuthenticated) Page.MasterPageFile = "~/Site-Ingelogd.Master";
            else
            {
                Page.MasterPageFile = "~/Site.Master";
                string url;
                url = "~/default.aspx";
                Response.Redirect(url);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            int reageerderID=0;
            String reageerder = Convert.ToString(Session["reageerder"]);
            SqlConnection sqlConn = new SqlConnection(Settings.Default.quaackdb);

            /*   Profileid opvragen van de ingelogde gebruiker   */
            SqlCommand GetreageerderID = new SqlCommand("SELECT profileid from profile where username = '" + reageerder + "'", sqlConn);


            /*   open database, haal de userID op en sluit database   */
            sqlConn.Open();
            object sqlObject = GetreageerderID.ExecuteScalar();
            if (sqlObject != null)
            {
                // userID zal uiteindelijk gebruikt worden om de sender aan te geven in de message tabel
                 reageerderID = Convert.ToInt32(sqlObject.ToString());
                
            }
            sqlConn.Close();

           
           
            Label2.Text = Convert.ToString(Session["reageerder"]);
            Session.Add("reageerderID", reageerderID);

         
        
        }


        protected void GridView1_RowCommand(object zender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "GaNaarGebruiker")
            {

                //string parentmessid = GridView1.SelectedRow.Cells[6].Text.Replace("&nbsp;", "0");
                string parentmessid = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[6].Text.Replace("&nbsp;", "0");


                //string sender = GridView1.SelectedRow.Cells[5].Text;
                string sender = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[5].Text.Replace("&nbsp;", "0");

                //string sender = GridView1.SelectedRow.Cells[2].Text;
                string reageerder = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[2].Text.Replace("&nbsp;", "0");
                Session["parentmessid"] = parentmessid;
                Session["sender"] = sender;
                Session["reageerder"] = reageerder;

                string url;
                url = "~/PaginaLezenAndereGebruiker.aspx";
                Response.Redirect(url);
            }

            if (e.CommandName == "Reageer")
            {

                // string parentmessid = GridView1.SelectedRow.Cells[5].Text.Replace("&nbsp;", "0");
                // string sender = GridView1.SelectedRow.Cells[4].Text;
                string messid = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[6].Text.Replace("&nbsp;", "0");
                string parentmessid = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[5].Text.Replace("&nbsp;", "0");
                string sender = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[4].Text.Replace("&nbsp;", "0");
                Session["messid"] = messid;
                Session["parentmessid"] = parentmessid;
                Session["sender"] = sender;
                
                string url;
                url = "~/reageer.aspx";
                Response.Redirect(url);

            }

            if (e.CommandName == "Blokkeer")
            {

                
                string parentmessid = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[6].Text.Replace("&nbsp;", "0");
                string sender = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[5].Text.Replace("&nbsp;", "0");
                Session["parentmessid"] = parentmessid;
                Session["sender"] = sender;
                string url;
                url = "~/BlokkeerDeBlokkeerVolger.aspx";
                Response.Redirect(url);

            }
        }


        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // hiermee controleren we of we te maken hebben met een reactie op een bericht, zo ja komt in de 1ste kolom de text reactie
            // tevens wil ik de reageer functie uitzetten op eigen berichten volgens mij is dit wenselijk
            // Check if row is data row

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Get value of fift column. Index is zero based, dit is de perrantmessid
                // to get text of fift column we use Cells[5].Text 


                if (string.IsNullOrWhiteSpace(e.Row.Cells[5].Text))
                {
                    e.Row.Cells[0].Text = "";
                   
                }
                else
                {
                    // als de cel een "&nbsp;" waarde heeft gaat de conversie mis, de "&nbsp;" vervang ik voor "0"
                    int CellValue = Convert.ToInt32(e.Row.Cells[5].Text.Replace("&nbsp;", "0"));

                    // If value is 1 then change to Yes, otherwise change to No
                    if (CellValue > 0)
                    {
                        e.Row.Cells[0].Text = "Reactie";
                    }
                }

            }
        }


    }
}